<?

require_once("engine/www/combustion.php");

$type		= $DB->simplestring( $_GET[ 'type' ]);
$columns	= json_decode( $_GET[ 'meta' ]);

$name = "csv.download - $type.csv";

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=\"" . $name . "\";");
#header("Content-Type: application/force-download");
		
foreach( $columns as $name => $column ) {
	$line[] = $name;
	if(  count( $sep 	= explode( ".", $column )) > 1 )
		 $meta[]		= array_pop( $sep );
	else $fields[]		= $column;
}
		
echo implode( ";", $line );
		
if( $objects = $DB->filter( $type, "active", false ))
			
	foreach( $objects as $obj ) {
			
		$line = array();
		if( $fields )	foreach( $fields as $column )	$line[] = $obj[ $column ]; 
		if( $meta ){	$obj[ "meta" ] = json_decode( $obj[ "meta" ]); 
						foreach( $meta as $column )		$line[] = $obj[ "meta" ]->{ $column };
		}
				
		echo "\n" . implode( ";", $line );
	}


?>